#include "Billboard.h"
#include "MyGL.h"

namespace Engine
{
	void DrawBillboard(const Vec3f& pos, float s)
	{
		glDepthMask(GL_FALSE);
		glDisable(GL_DEPTH_TEST);
		glDisable(GL_LIGHTING);
		glEnable(GL_BLEND);

		//query opengl for model view matrix
		float f[16];
		glGetFloatv(GL_MODELVIEW_MATRIX, f);

		//work out x(right), up(y) and in(z) vectors by transposing rotation part
		Vec3f right, up, in;
		right.x = f[0]; right.y = f[4]; right.z = f[8];
		up.x =    f[1]; up.y =    f[5];    up.z = f[9];
		in.x =    f[2]; in.y =    f[6];    in.z = f[10];

		//working out all vertexes for quads
		Vec3f topLeft, bottomLeft, bottomRight, topRight;

		topLeft = pos + (-right + up) * s;
		bottomLeft = pos + (-right - up) * s;
		bottomRight = pos + (right - up) * s;
		topRight = pos + (right + up) * s;

		//drawing
		glBegin(GL_QUADS);
			glNormal3fv(in.ToArray());

			glTexCoord2f(0.0f, 0.0f);
			glVertex3fv(topLeft.ToArray());

			glTexCoord2f(0.0f, 1.0f);
			glVertex3fv(bottomLeft.ToArray());

			glTexCoord2f(1.0f, 1.0f);
			glVertex3fv(bottomRight.ToArray());

			glTexCoord2f(1.0f, 0.0f);
			glVertex3fv(topRight.ToArray());
		glEnd();

		glEnable(GL_LIGHTING);
		glDisable(GL_BLEND);
		glDepthMask(GL_TRUE);
		glEnable(GL_DEPTH_TEST);
	}
}

